/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * GUIRemoveCliente.java
 *
 * Created on 26/11/2011, 16:18:24
 */

package GUI;



import javax.swing.table.DefaultTableModel;

import tp1POO.Verifica;
import tp1POO.Persistencia.GerenciadorObjetos;
import tp1POO.Persistencia.Pesquisa;
import tp1POO.Modelo.*;
import java.util.Vector;
import javax.swing.JOptionPane;
import tp1POO.GestorLog;

    // Variables declaration - do not modify
/**
 *
 * @author fernando
 */
public class GUIRemoveCliente extends javax.swing.JFrame {

    /** Creates new form GUIRemoveCliente */
    private String msg = "";

    public GUIRemoveCliente() {

        initComponents();
        preencherJTable();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTableRemove = new javax.swing.JTable();
        jCancel = new javax.swing.JButton();
        jRemove = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("Remover Cliente");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Selecione o cliente a ser removido", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Dialog", 1, 14))); // NOI18N

        jTableRemove.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Nome", "CPF", "Endereço", "Telefone"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        jTableRemove.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane1.setViewportView(jTableRemove);
        jTableRemove.getColumnModel().getSelectionModel().setSelectionMode(javax.swing.ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 375, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(51, Short.MAX_VALUE))
        );

        jCancel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/icoErro.gif"))); // NOI18N
        jCancel.setText("Fechar");
        jCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCancelActionPerformed(evt);
            }
        });

        jRemove.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/deletar.gif"))); // NOI18N
        jRemove.setText("Remover");
        jRemove.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRemoveActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jRemove, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jCancel, javax.swing.GroupLayout.DEFAULT_SIZE, 111, Short.MAX_VALUE))
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jCancel, jRemove});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(41, 41, 41)
                        .addComponent(jRemove)
                        .addGap(36, 36, 36)
                        .addComponent(jCancel))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {jCancel, jRemove});

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void erroRemoveCliente(String msg){

        JOptionPane.showMessageDialog(jTableRemove,msg, "Error", JOptionPane.ERROR_MESSAGE);

    }


    private void jRemoveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRemoveActionPerformed
        DefaultTableModel modelo = (DefaultTableModel)jTableRemove.getModel();
        int linha = jTableRemove.getSelectedRow();
        String msg="";

        if (jTableRemove.getSelectedRow()>=0){
                String nome = (String) jTableRemove.getValueAt(linha, 0);

                Cliente cliente = Pesquisa.ObterClienteNome(nome);

                if (cliente != null) {
                    msg = msg + Verifica.dependenciaCliente(cliente.getNome());
                    if (msg.isEmpty()) {
                        modelo.removeRow(linha);
                        GestorLog.obterInstancia().inserirMsgLog(GuiCreator.obterInstancia().getGuiFuncionalidades().getFuncionario().getCpf()+" - Cliente removido com sucesso!\n\tNome : "+
                                cliente.getNome()+"\n\tCPF : "+cliente.getCpf());
                        JOptionPane.showMessageDialog(jPanel1,"Cliente removido com Sucesso!", "Sucesso", JOptionPane.INFORMATION_MESSAGE);
                    }
                    else {
                        erroRemoveCliente(msg);
                    }
                }
        }
        else{
            erroRemoveCliente("ERRO : Nenhum cliente foi selecionado!");
        }
    }//GEN-LAST:event_jRemoveActionPerformed

    private void jCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCancelActionPerformed
        GestorLog.obterInstancia().inserirMsgLog("Funcionalidade de Remover clientes foi fechada("+GuiCreator.obterInstancia().getGuiFuncionalidades().getFuncionario().getCpf()+")");
        GuiCreator.obterInstancia().removerGuiRemCliente();
    }//GEN-LAST:event_jCancelActionPerformed

    /**
    * @param args the command line arguments
    */


    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new GUIRemoveCliente().setVisible(true);
            }
        });
    }

    private void preencherJTable(){
       DefaultTableModel modelo = (DefaultTableModel)jTableRemove.getModel();
       modelo.setNumRows(0);
       Cliente cli = new Cliente();
       Vector<ObjetoPOO> cliente = GerenciadorObjetos.obterInstancia().obterObjs(cli);

     for (int i=0;i<cliente.size();i++){
           cli = (Cliente) cliente.get(i);
           modelo.addRow(new Object[] {cli.getNome(),cli.getCpf(),cli.getEndereco(),cli.getTelefone()});
       }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jCancel;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JButton jRemove;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTableRemove;
    // End of variables declaration//GEN-END:variables

}
